package com.aos.util;

import com.aos.dto.UserDto;
import com.aos.properties.APMSProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName UserUtils
 * @Description:用户工具类
 * @Author wumingwang
 * @Date 2018/9/14 10:26
 * @Version 1.0
 */
public class UserUtils {

    public static String getToken(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader("Authorization");
        token = token.replace("Bearer","").trim();
        return token;
    }

    public static UserDto getCurrentUser(String key){
        RedisUtils redisUtils = (RedisUtils)SpringUtils.getBean("redisUtils");
        Object o = redisUtils.get(key);
        if(o != null){
            return (UserDto)o;
        }else{
            return null;
        }
    }

    public static UserDto getCurrentUser(){
        APMSProperties apms = (APMSProperties)SpringUtils.getBean("aPMSProperties");
        return getCurrentUser(getToken()+apms.getUser().getRedisKey());
    }
}
